

import { Direction2D } from "shared/enums/UtilValue";

import TriggerBase from "./TriggerBase";
import { Direction2DValue } from "../robloxex/ValueObject";
import Log4R from "../log/Log4R";
const loginfo = Log4R.getLog('info');
/**
 * 墙头属性
 */
interface WallHeaderConfg extends Configuration {
    direction: Direction2DValue

}
/**
 * 墙头
 */
export default class WallHeader extends TriggerBase implements clazz.WallHeader {
    /** 抓取用的att */
    catchAttachment: Attachment;
    /**
     * 添加 墙头
     * @param part 
     * @param directionValue 
     */
    static addWallHead(part: Instance, directionValue: Direction2D) {
        const triggerTag = new Instance('Configuration');
        triggerTag.Name = "WallHeader";
        const direction = new Instance('StringValue') as Direction2DValue;
        direction.Name = "direction";
        direction.Value = directionValue;
        direction.Parent = triggerTag
        triggerTag.Parent = part;
        const catchAttachment = new Instance('Attachment');
        catchAttachment.Parent = part;
        catchAttachment.CFrame = CFrame.fromOrientation(0, math.rad(90), 0);
        catchAttachment.Name = "catchAttachment";
    }
    onTouch(call: (c: BasePart) => void) {
        this.toucher?.Touched.Connect((c) => {
            // 添加触碰条件
            // loginfo?.info(c.CFrame.ToOrientation(), this.config.direction.Value)
            const headOrientaion = c.CFrame.ToOrientation();
            let success = false
            if (headOrientaion[1] > 1 && this.config.direction.Value === Direction2D.Left) {
                success = true;
            } else if (headOrientaion[1] < -1 && this.config.direction.Value === Direction2D.Right) {
                success = true;
            }
            if (success) {
                call(c)
            }

        })
    }
    declare config: WallHeaderConfg;
    constructor(config: Configuration) {
        super(config);
        this.setTag('WallHeader');
        if (this.instance?.IsA('BasePart')) {
            this.toucher = this.instance
        }
        this.catchAttachment = this.instance?.FindFirstChild('catchAttachment') as Attachment;
    }

}